package com.dsp.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.core.util.DspUtil;
import com.core.util.Pagination;
import com.dsp.bean.InsureResultsSub;
import com.dsp.services.InsureResultsSubService;

@Controller("InsureResultsSubsController")
public class InsureResultsSubsController extends DspController<InsureResultsSub> {

	public InsureResultsSubsController() {
		super(InsureResultsSub.class);
	}

	Logger logger = Logger.getLogger(DspUtil.Log4J);
	
	@Autowired
	private InsureResultsSubService insureResultsSubService ;
	
	@RequestMapping( value = "/insure_results_subs" , method = RequestMethod.GET)
	public ModelAndView index(){
		ModelAndView mav = new ModelAndView();
		mav.setViewName("insure_results_sub/list");
		mav.addObject("path", "insure_results_sub/list.jsp");
		mav.addObject("current", "insure_results_subs");
		return mav;
	}
	@RequestMapping(value = "/insure_results_subs_list" , method = RequestMethod.POST)
	@ResponseBody
	public Pagination<InsureResultsSub> index(HttpServletRequest request, HttpServletResponse response){
		int page = Integer.valueOf(request.getParameter("page"));
		int rp = Integer.valueOf(request.getParameter("rp"));
		String column = request.getParameter("qtype");
		String value = request.getParameter("query");

		DetachedCriteria dc = DetachedCriteria.forClass(InsureResultsSub.class);
		if (!DspUtil.isEmpty(column) && !DspUtil.isEmpty(value)) {
			dc.add(Restrictions.like(column, value, MatchMode.ANYWHERE));
		}
		Pagination<InsureResultsSub> p = this.insureResultsSubService.search(dc, (page - 1) * rp, rp);
		p.setPage(page);
		return p;
	}

	@RequestMapping(value ="/insure_results_subs_query", method = RequestMethod.POST)
	@ResponseBody
	public Pagination<InsureResultsSub> query(HttpServletRequest request , HttpServletResponse response){
		super.initHttp(request, response);
		String column = request.getParameter("qtype");
		Long value = Long.valueOf(request.getParameter("query"));
		DetachedCriteria dc = DetachedCriteria.forClass(InsureResultsSub.class);
		dc.add(Restrictions.eq(column, value));
		Pagination<InsureResultsSub> p = this.insureResultsSubService.search(dc, (page - 1) * rp , rp);
		p.setFooter(this.insureResultsSubService.setFooter(p.getRows()));
		p.setPage(page);
		return p;
	}
	
	/**
	 * @return the insureResultsSubService
	 */
	public InsureResultsSubService getInsureResultsSubService() {
		return insureResultsSubService;
	}

	/**
	 * @param insureResultsSubService the insureResultsSubService to set
	 */
	public void setInsureResultsSubService(InsureResultsSubService insureResultsSubService) {
		this.insureResultsSubService = insureResultsSubService;
	}
	
	
}
